<?php

namespace Drupal\shushengyuan\Schemas;

use Drupal\shusy_store_order\Entity\ShusyStoreOrder;
use Drupal\shusy_store_order_extension\Entity\ShusyStoreOrderExtension;

/**
 * @OA\Schema(
 *   schema="OrderInfo",
 *   type="object",
 *   description="OrderInfo",
 *   allOf={
 *       @OA\Schema(ref="#/components/schemas/MyOrderItem"),
 *   }
 * )
 */
class OrderInfo extends MyOrderItem {

  protected function setValue() {
    parent::setValue();
    if ($this->entity instanceof ShusyStoreOrder) {
      $value = [
        /**
         * @OA\Property(
         *   property="point",
         *   type="integer",
         *   description="可获得积分数量",
         * ),
         */
        'point' => $this->getPoint($this->entity),
        /**
         * @OA\Property(
         *   property="createdTime",
         *   type="string",
         *   description="创建时间 空字符串不显示",
         * ),
         */
        'createdTime' => $this->formatDate($this->entity->created, 'Y-m-d H:i:s'),
        /**
         * @OA\Property(
         *   property="payTime",
         *   type="string",
         *   description="付款时间 空字符串不显示",
         * ),
         */
        'payTime' => $this->getPayTime($this->entity),
        /**
         * @OA\Property(
         *   property="shippingTime",
         *   type="string",
         *   description="发货时间 空字符串不显示",
         * ),
         */
        'shippingTime' => $this->getShippingTime($this->entity),
        /**
         * @OA\Property(
         *   property="completeTime",
         *   type="string",
         *   description="成交时间 空字符串不显示",
         * ),
         */
        'completeTime' => $this->getCompleteTime($this->entity),
      ];
      $this->merge($value);

      //订单类型 0 正常 1 拼团 2 秒杀
      switch ($this->entity->type) {
        case 0:
          break;
        case 1:
          //附加拼团信息
          $value = [
            /**
             * @OA\Property(
             *   property="pting",
             *   ref="#/components/schemas/ProductPtingItem",
             *   description="拼团信息",
             * ),
             */
            'pting' => ProductPtingItem::get($this->wrapper->player->pintuan->id->value()),
          ];
          $this->merge($value);
          break;
        case 2:
          break;
      }

    }
  }

  protected function getPoint(ShusyStoreOrder $order) {
    $es = shusy_store_order_extension_load_multiple(FALSE, ['store_order_id' => $order->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreOrderExtension) {
        return $e->point;
      }
    }
    return 0;
  }

  protected function getPayTime(ShusyStoreOrder $order) {
    $es = shusy_store_order_extension_load_multiple(FALSE, ['store_order_id' => $order->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreOrderExtension && $e->pay_time > 0) {
        return $this->formatDate($e->pay_time, 'Y-m-d H:i:s');
      }
    }
    return '';
  }

  protected function getShippingTime(ShusyStoreOrder $order) {
    $es = shusy_store_order_extension_load_multiple(FALSE, ['store_order_id' => $order->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreOrderExtension && $e->shipping_time > 0) {
        return $this->formatDate($e->shipping_time, 'Y-m-d H:i:s');
      }
    }
    return '';
  }

  protected function getCompleteTime(ShusyStoreOrder $order) {
    $es = shusy_store_order_extension_load_multiple(FALSE, ['store_order_id' => $order->id]);
    if (!empty($es)) {
      $e = reset($es);
      if ($e instanceof ShusyStoreOrderExtension && $e->complete_time > 0) {
        return $this->formatDate($e->complete_time, 'Y-m-d H:i:s');
      }
    }
    return '';
  }
}
